package susebs.pb.travel_server.service.impl;

import org.springframework.stereotype.Service;
import susebs.pb.travel_server.domain.HotelRoom;
import susebs.pb.travel_server.domain.NavigationResponse;
import susebs.pb.travel_server.entity.*;
import susebs.pb.travel_server.service.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 导航业务层
 */
@Service
public class NavigationsServiceImpl implements NavigationsService {

    @Resource
    private LandmarksService landmarksService;
    @Resource
    private HotelsService hotelsService;
    @Resource
    private RoomsService roomsService;
    @Resource
    private PicturesService picturesService;

    /**
     * 根据传来的list查询其对应的图片，重组成新的结果集NavigationResponse返回给控制层
     * 因为
     * @param list 景点/酒店/房间列表
     * @return NavigationResponse导航结果集
     */
    @Override
    public <T>ArrayList<NavigationResponse> getListsAndPics(List<T> list) {

        NavigationResponse response;
        ArrayList<NavigationResponse> res = new ArrayList<>();

        //遍历list取出id，进行图片查询和结果集生成
        for (T data : list) {
            Long id;
            Pictures picture;
            response = new NavigationResponse();

            //取参查询对应图片
            if (data.getClass().equals(Hotels.class)) {
                id = ((Hotels) data).getHotelId();
                try {
                    picture = picturesService.queryPicturesByOwner(1, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }

                response.setHotels((Hotels) data);
                response.setPictures(picture);

                res.add(response);
            }
            else if (data.getClass().equals(HotelRoom.class)) {
                id = ((HotelRoom) data).getRooms().getRoomId();
                try {
                    picture = picturesService.queryPicturesByOwner(2, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }

                response.setRooms(((HotelRoom) data).getRooms());
                response.setPictures(picture);
                response.setHotelName(((HotelRoom) data).getHotelName());

                res.add(response);
            }
            else if (data.getClass().equals(Rooms.class)) {
                id = ((Rooms) data).getRoomId();
                try {
                    picture = picturesService.queryPicturesByOwner(2, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }

                response.setRooms((Rooms) data);
                response.setPictures(picture);

                res.add(response);
            }
            else if (data.getClass().equals(Landmarks.class)) {
                id = ((Landmarks) data).getLmId();
                try {
                    picture = picturesService.queryPicturesByOwner(3, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }

                response.setLandmarks((Landmarks) data);
                response.setPictures(picture);

                res.add(response);
            }
            else if (data.getClass().equals(Users.class)) {
                id = ((Users) data).getUserId();
                try {
                    picture = picturesService.queryPicturesByOwner(4, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }
                response.setUsers((Users) data);
                response.setPictures(picture);

                res.add(response);
            }
            else if (data.getClass().equals(Comments.class)) {
                id = ((Comments) data).getCommentId();
                try {
                    picture = picturesService.queryPicturesByOwner(5, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }

                response.setComments((Comments) data);
                response.setPictures(picture);

                res.add(response);
            }
            else if (data.getClass().equals(Packages.class)) {
                id = ((Packages) data).getPkId();
                try {
                    picture = picturesService.queryPicturesByOwner(6, id).get(0);
                } catch (Exception e){
                    picture = new Pictures();
                }

                response.setPackages(((Packages) data));
                response.setPictures(picture);

                res.add(response);
            }
        }

        return res;
    }
}
